package com.lzx.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.answer.entity.Problem;
import com.lzx.answer.entity.Reply;
import com.lzx.answer.entity.vo.NicknameVO;
import com.lzx.answer.entity.vo.OssVO;
import com.lzx.answer.feign.UserClient;
import com.lzx.answer.mapper.ProblemMapper;
import com.lzx.answer.mapper.ReplyMapper;
import com.lzx.answer.service.ReplyService;
import com.lzx.common.entity.Inform;
import com.lzx.common.enums.InformType;
import com.lzx.common.utils.R;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2022-10-05
 */
@Service
public class ReplyServiceImpl extends ServiceImpl<ReplyMapper, Reply> implements ReplyService {
    @Resource
    private ReplyMapper replyMapper;

    @Resource
    private ProblemMapper problemMapper;

    @Resource
    private UserClient userClient;

    //问题的回复
    @GlobalTransactional
    @Override
    public R reply(Reply reply) {
        reply.setCreateTime(new Date());
        replyMapper.insert(reply);

        //问题评论数+1
        Problem problem = problemMapper.selectById(reply.getProblemId());
        problem.setReply(problem.getReply() + 1);
        problemMapper.updateById(problem);

        //回答人回答数+1
        userClient.userReplyAdd(reply.getUserId());

        //发送通知
        Inform inform = new Inform();
        inform.setUserId(problem.getUserId());
        inform.setSender(Math.toIntExact(reply.getUserId()));
        inform.setNickname(reply.getNickname());
        inform.setAvatar(reply.getAvatar());
        inform.setType(InformType.COMMENTPROBLEM.getType());
        inform.setMessage(problem.getTitle());
        userClient.saveInform(inform);

        return R.ok().message("回复成功！");
    }

    //获取当前问题的一级评论
    @Override
    public R getLevel1Comments(Long problemId) {
        QueryWrapper<Reply> wrapper = new QueryWrapper<>();
        wrapper.eq("problem_id", problemId).orderByDesc("is_top");
        List<Reply> level1Comments = replyMapper.selectList(wrapper);
        return R.ok().data("list", level1Comments);
    }

    //问题回复置顶
    @Override
    public R replyTop(Long problemId, Long replyId) {
        QueryWrapper<Reply> wrapper = new QueryWrapper<>();
        wrapper.eq("problem_id", problemId).eq("id", replyId);
        Reply reply = replyMapper.selectOne(wrapper);

        //查询当前置顶字段的最大值
        QueryWrapper<Reply> replyQueryWrapper = new QueryWrapper<>();
        replyQueryWrapper.orderByDesc("is_top").last("limit 1");
        Reply vo = replyMapper.selectOne(replyQueryWrapper);

        reply.setIsTop(vo.getIsTop() + 1);
        replyMapper.updateById(reply);
        return R.ok().message("置顶成功");
    }

    //热评
    @Override
    public R hopReply() {
        QueryWrapper<Reply> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("like_count").last("limit 5");
        List<Reply> list = replyMapper.selectList(wrapper);
        return R.ok().data("list", list);
    }

    //修改用户头像(问题一级回复)
    @Transactional
    @Override
    public String updateAvatarToReply(OssVO ossVO) {
        QueryWrapper<Reply> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", ossVO.getId());
        List<Reply> replyList = replyMapper.selectList(wrapper);
        for (Reply reply : replyList) {
            reply.setAvatar(ossVO.getAvatar());
            replyMapper.updateById(reply);
        }
        return "修改成功";
    }

    //修改用户昵称(问题一级回复)
    @Transactional
    @Override
    public String updateNicknameToReply(NicknameVO nicknameVO) {
        QueryWrapper<Reply> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", nicknameVO.getId());
        List<Reply> replyList = replyMapper.selectList(wrapper);
        for (Reply reply : replyList) {
            reply.setNickname(nicknameVO.getNickname());
            replyMapper.updateById(reply);
        }
        return "修改成功";
    }
}
